package com.example.dropoff;

import java.util.ArrayList;

import android.app.Activity;
import android.content.Intent;
import android.database.Cursor;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.WindowManager;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.AdapterView.OnItemLongClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageButton;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;


public class list extends Activity implements OnClickListener, OnItemClickListener
{
   
	private static final String TAG = "list";
	protected static list mcontext;
	private DbOpenHelper mDbOpenHelper;
	private Cursor mCursor;
	private InfoClass mInfoClass;
	private ArrayList<InfoClass> mInfoArray;
	private CustomAdapter mAdapter;
	// 추가될 아이템 내용을 입력받는 EditText
 
    
   // 아이템 추가 버튼
   private Button mBInputToList;
    
   // 리스트뷰
   private ImageButton imgbtn;
    
   // 데이터 리스트

    
   // 리스트뷰에 사용되는 ArrayAdapter
  
    
   @Override
   public void onCreate(Bundle savedInstanceState)
   {
       super.onCreate(savedInstanceState);
       setContentView(R.layout.list_list);
       setLayout();
       
       // DB Create and Open
       mDbOpenHelper = new DbOpenHelper(this);
       mDbOpenHelper.open();
        
    
              
//       startManagingCursor(mCursor);
        
        
       mInfoArray = new ArrayList<InfoClass>();
       
 
       doWhileCursorToArray();
       
       for(InfoClass i : mInfoArray){
       	DLog.d(TAG, "ID = " + i._id);
       	DLog.d(TAG, "NAME= : " + i.name);
       	DLog.d(TAG, "DATE_YY = :" + i.date_yy);
       	DLog.d(TAG, "DATE_DD = :" + i.date_dd);
       	DLog.d(TAG, "DATE_MM = :" + i.date_mm);
       	DLog.d(TAG, "AMOUNT = " + i.amount);
    	DLog.d(TAG, "UNIT =  :" + i.unit);
    	DLog.d(TAG, "MEMO =  :" + i.memo);
       }
       
       mAdapter = new CustomAdapter(this, mInfoArray);
       mListView.setAdapter(mAdapter);
       mListView.setOnItemLongClickListener(longClickListener);

       mListView.setOnItemClickListener(this);
       //////////////////////////////////////////////////////////////
       // 위젯 레퍼런스 시작
        
       
     
       addListenerOnButton();
  
   }
   public void addListenerOnButton() {
	   imgbtn = (ImageButton) findViewById(R.id.plusbutton02);
	   imgbtn.setOnClickListener(new OnClickListener() {

           @Override
           public void onClick(View arg0) {

               Intent i = new Intent
                       (getApplicationContext(), popupactivity1.class);
                   startActivity(i); 
           }
       });
   }
   
   protected void onDestroy() {
   	mDbOpenHelper.close();
   	super.onDestroy();
   }
   
   

   private OnItemLongClickListener longClickListener = new OnItemLongClickListener() {
		@Override
		public boolean onItemLongClick(AdapterView<?> arg0, View arg1, int position, long arg3) {
			
			DLog.e(TAG, "position = " + position);
			
			boolean result = mDbOpenHelper.deleteColumn(position + 1);
			DLog.e(TAG, "result = " + result);
			
			if(result){
				mInfoArray.remove(position);
				mAdapter.setArrayList(mInfoArray);
				mAdapter.notifyDataSetChanged();
			}else {
				Toast.makeText(getApplicationContext(), "INDEX오류", 
						Toast.LENGTH_LONG).show();
			}
			
			return false;
		}
	};
	
	public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
	//	  Toast.makeText(this,"ListNo " + value[position*2 + 1], Toast.LENGTH_SHORT).show();

/*		  TextView tv = (TextView)view.findViewById(R.id.alarm_name);
		  Intent intent = new Intent(this, Friend.class);
		  intent.putExtra("listNo", Integer.valueOf(value[position*2 + 1]));
		  intent.putExtra("alarm_name", tv.getText());
		  startActivity(intent);
		  */
		InfoClass info = mInfoArray.get(position);
		Toast.makeText(this, info._id +", "+info.name, Toast.LENGTH_SHORT).show();
		
		Intent intent = new Intent(this, DetailActivity.class);
		intent.putExtra("_id",info._id);
		startActivity(intent);
	}
	

	public void doWhileCursorToArray(){
		
		mCursor = null;
		mCursor = mDbOpenHelper.getAllColumns();
		DLog.e(TAG, "COUNT = " + mCursor.getCount());
		
		while (mCursor.moveToNext()) {
       	
			mInfoClass = new InfoClass(
					mCursor.getInt(mCursor.getColumnIndex("_id")),
					mCursor.getString(mCursor.getColumnIndex("name")),
					mCursor.getString(mCursor.getColumnIndex("date_yy")),
					mCursor.getString(mCursor.getColumnIndex("date_mm")),
					mCursor.getString(mCursor.getColumnIndex("date_dd")),
					mCursor.getString(mCursor.getColumnIndex("amount")),
					mCursor.getString(mCursor.getColumnIndex("unit")),
					mCursor.getString(mCursor.getColumnIndex("memo"))
					);
			
			mInfoArray.add(mInfoClass);
		}
		
		mCursor.close();
	}
   @Override

   protected void onResume(){
	    super.onResume();
	    	
	    getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_HIDDEN);
	}
   public void onClick(View v)
   {
       
	   
		
   }
   
   /*
    * Layout
    */
   private EditText[] mEditTexts;
   
   private ListView mListView;
   
   private void setLayout(){
   	mEditTexts = new EditText[]{
   			(EditText)findViewById(R.id.et_name),
   			
   	};
   	
   	mListView = (ListView) findViewById(R.id.main_lv_list);
   }
}